package me.obstsalat.guildera;

import org.bukkit.block.Block;
import org.bukkit.entity.ExperienceOrb;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.inventory.BrewEvent;
import org.bukkit.event.inventory.FurnaceSmeltEvent;
import org.bukkit.event.player.PlayerExpChangeEvent;

public class g_experience {
	
	private static guildera plugin;
	
	g_experience(guildera pl) {
		plugin = pl;
	}
	
	
	public boolean onBrew(BrewEvent e) {
		if(!plugin.checkViableWorld(e.getBlock().getWorld())) {
			return false;
		}
		plugin.overworld.spawn(e.getBlock().getLocation().add(0,1,0), ExperienceOrb.class);
		plugin.overworld.spawn(e.getBlock().getLocation().add(0,1,0), ExperienceOrb.class);
		plugin.overworld.spawn(e.getBlock().getLocation().add(0,1,0), ExperienceOrb.class);
		return false;
	}
	
	public boolean onPlayerDeath(PlayerDeathEvent event)  {
		if(!plugin.checkViableWorld(event.getEntity().getWorld())) {
			return false;
		}
		int ep = event.getDroppedExp();
		event.setDroppedExp(0);
		event.setNewTotalExp(ep);
		return false;
	}
	
	public boolean onPlayerExpChange(PlayerExpChangeEvent e) {
		if(!plugin.checkViableWorld(e.getPlayer().getWorld())) {
			return false;
		}
		if(e.getAmount() > 0) {
			for(guild g : plugin.guilds) {
				if(g.hasMember(e.getPlayer().getName())) {
					int lvl = g.getLevel();
					int am = (int)(e.getAmount()*plugin.config.getDouble("monster_exp_ratio"));
					g.addEp(am);
					if(lvl < g.getLevel()) {
						g.save(plugin);
					}
				}
			}
		}
		return false;
	}
	
	public boolean onFurnaceSmelt(FurnaceSmeltEvent event) {
		if(event.getResult().getTypeId() == 265) {
			for(guild g : plugin.guilds) {
				if(g.isInGuild(event.getBlock().getLocation())) {
					g.addEp(plugin.config.getInt("iron_exp"));
					g.announce("Guild got " + plugin.config.getInt("iron_exp") + " exp for melting iron.");
				}
			}
		} else if(event.getResult().getTypeId() == 266) {
			for(guild g : plugin.guilds) {
				if(g.isInGuild(event.getBlock().getLocation())) {
					g.addEp(plugin.config.getInt("gold_exp"));
					g.announce("Guild got " + plugin.config.getInt("gold_exp") + " exp for melting gold.");
				}
			}
		}
		return false;
	}
	
	public boolean onBlockBreak(BlockBreakEvent event) {
		Block bl = event.getBlock();
		if(!plugin.checkViableWorld(bl.getWorld())) {
			return false;
		}
		if(!plugin.overworld.getName().equals(event.getBlock().getWorld().getName())) {
			return false;
		}
		if(bl.getTypeId() == 16) {
			for(guild g : plugin.guilds) {
				if(g.hasMember(event.getPlayer().getName())) {
					g.addEp(plugin.config.getInt("coal_exp"));
					event.getPlayer().sendMessage("Your guild got " + plugin.config.getInt("coal_exp") + " exp for mining coal.");
				}
			}
		} else if(bl.getTypeId() == 56) {
			for(guild g : plugin.guilds) {
				if(g.hasMember(event.getPlayer().getName())) {
					g.addEp(plugin.config.getInt("diamond_exp"));
					event.getPlayer().sendMessage("Your guild got " + plugin.config.getInt("diamond_exp") + " exp for mining a diamond.");
				}
			}
		} else if(bl.getTypeId() == 73 || bl.getTypeId() == 74) {
			for(guild g : plugin.guilds) {
				if(g.hasMember(event.getPlayer().getName())) {
					g.addEp(plugin.config.getInt("redstone_exp"));
					event.getPlayer().sendMessage("Your guild got " + plugin.config.getInt("redstone_exp") + " exp for mining redstone.");
				}
			}
		} else if(bl.getTypeId() == 21) {
			for(guild g : plugin.guilds) {
				if(g.hasMember(event.getPlayer().getName())) {
					g.addEp(plugin.config.getInt("lazulli_exp"));
					event.getPlayer().sendMessage("Your guild got " + plugin.config.getInt("lazulli_exp") + " exp for mining a lapis lazulli.");
				}
			}
		}
		return false;
	}
}
